package client;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.List;
import java.util.Scanner;

import remoteinfo.MetaInfo;
import remoteinfo.ResultSet;
public class Client {
	public static void display(ResultSet rs){
		if(rs.cmdtype == 0){
			int isize = rs.fld.length;
			int jsize = rs.val[0].length;
			for(int i=0;i<isize;i++){
				System.out.print(rs.fld[i]+"	");
			}
			System.out.println();
			for(int i=0;i<jsize;i++){
				for(int j=0;j<isize;j++)
					System.out.print(rs.val[j][i]+"	");
				System.out.println();
			}
			System.out.println();
		}
		else{
			System.out.println("update successfully");
		}
	}
	
	public static void main(String[] args){
		try{
			Socket socket = new Socket("127.0.0.1",5678);
			PrintWriter out = new PrintWriter(socket.getOutputStream());
			BufferedReader sin = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
			Scanner cin = new Scanner(System.in);
			String s;
			do{
				s = cin.nextLine();
				String[] ss = s.split(",");
				out.println(s);
				out.flush();
				if(s.equals("metainfo")){
					List<MetaInfo> milist = (List<MetaInfo>)ois.readObject();
					for(MetaInfo mi:milist){
						System.out.println(mi);
					}
					continue;
				}
				else if(ss[0].equals("userlogin")){
					continue;
				}
				ResultSet rs = (ResultSet)ois.readObject();
				if(rs.success == 1){
					display(rs);
				}
				else{
					System.out.println(rs.errorMsg);
				}
			}while(! s.equals("bye"));
			out.close();
			sin.close();
			socket.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
